import { Component } from 'react'
import { List } from './list'
import NP from 'number-precision'
import './App.css'

export default class App extends Component {
  state = {
    list: [
      {
        id: 1,
        name: '超级好吃的棒棒糖',
        price: 18.8,
        info: '开业大酬宾，全场8折',
      },
      {
        id: 2,
        name: '超级好吃的大鸡腿',
        price: 34.2,
        info: '开业大酬宾，全场8折',
      },
      {
        id: 3,
        name: '超级无敌的冰激凌',
        price: 14.2,
        info: '开业大酬宾，全场8折',
      },
    ],
  }

  // 子组件砍价
  kanJia = (id, price) => {
    const newList = this.state.list.map((item) => {
      let newPrice = NP.minus(item.price, price)
      if (newPrice <= 0) {
        newPrice = 0
      }
      if (item.id === id) {
        return {
          ...item,
          price: newPrice,
        }
      }
      return item
    })
    this.setState({
      list: newList,
    })
  }
  render() {
    const { list } = this.state
    return (
      <div className="parent">
        <h1>父组件</h1>
        {list.map((item) => {
          return <List {...item} kanJia={this.kanJia}></List>
        })}
      </div>
    )
  }
}
